home *** CD-ROM | disk | FTP | other *** search
- /*====================================================================*/
- /* */
- /* FP_main.c: XObject interface to our floating-point package. */
- /* Hopefully we can put all of the XObject-ish stuff */
- /* in this file and keep everything else generic. */
- /* */
- /* Copyright ⌐ 1992 The Regents of the University of California. */
- /* */
- /* 3/21/92 SK Just getting started... */
- /* 3/31/92 SK First functional version (sine function) */
- /* 4/4/92 SK Adding other functions... */
- /* */
- /*====================================================================*/
-
- #include <sane.h>
-
- /*-----------------------------------------------*/
- /* Stuff currently missing from the SANE header: */
- /*-----------------------------------------------*/
- #define exp21(x) _elems1(FEXP21X,x)
- #define log21(x) _elems1(FLOG21X,x)
- #define ln1(x) _elems1(FLN1X,x)
- #define ln(x) _elems1(FLNX,x)
-
-
- /*-------------------------------------------------------------------*/
- /* The XObject include file contains macros that make it easier to */
- /* declare the structures necessary to build an XObject. */
- /* This header is supplied by MacroMind/Paracomp in the XObject */
- /* developer's toolkit. */
- /*-------------------------------------------------------------------*/
- #include <XObject.h>
- #define TY_EXTENDED 9 /* 80-bit extended float passed as variable arg */
-
- enum { FN_Log2, FN_Ln, FN_Ln1, FN_Exp2, FN_Exp, FN_Exp1, FN_Cos,
- FN_Sin, FN_Tan, FN_Atan, FN_RandomX, FN_Log21, FN_Exp21 };
-
- /*----------------------------------------------------------------------*/
- /* This declaration defines the structure of the data in the XObject, */
- /* that is, each instance of the XObject will have these fields for */
- /* data storage. Note that XClassBegin and XClassEnd are macros */
- /* defined in XObject.h */
- /*----------------------------------------------------------------------*/
- XClassBegin {
- long temp; /* I don't know what data I need yet, so this is a placeholder! */
- } XClassEnd FPObj, FPObjRec;
-
-
- /*----------------------------------------------------------------------*/
- /* Function prototypes */
- /*----------------------------------------------------------------------*/
- void main(void);
- pascal long MethodNew(MsgId message, FPObj me);
- pascal long MethodLog2(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodLn(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodLn1(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodExp2(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodExp(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodExp1(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodCos(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodSin(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodTan(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodAtan(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodRandomX(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodLog21(long argc, TyValuePtr argv, MsgId message, FPObj me);
- pascal long MethodExp21(long argc, TyValuePtr argv, MsgId message, FPObj me);
-
-
- /*----------------------------------------------------------------------*/
- /* main() provides the entry point for our code resource. It contains */
- /* the message table, which describes the methods and arguments that */
- /* callers can use, and provides a jump table for the C code to jump */
- /* to those messages. Note that XMessagesBegin, XMessage, */
- /* XMessageEnd, XMethodsBegin, XMethod, and XMethodsEnd are all */
- /* macros that provide the assembly code to correctly build the entry */
- /* point for the code resource. See XObject.h for more info. */
- /* WARNING: MAIN MUST BE THE FIRST FUNCTION IN THIS FILE. OTHERWISE */
- /* THE ENTRY CODE FOR THE CODE RESOURCE WON'T BE SET UP RIGHT!!! */
- /*----------------------------------------------------------------------*/
- void main(void)
- {
- /*--- message name table ---*/
- XMessagesBegin
- XMessage "\p-- FP, an XObject to provide advanced floating point functions.";
- XMessage "\p-- by Scott Kelley 4/4/92";
- XMessage "\p-- Copyright ⌐ 1992 The Regents of the University of California"
- XMessage "\pI mNew -- create a new instance";
- XMessage "\pV mLog2 -- computes the base-2 logarithm of x";
- XMessage "\pV mLn -- computes the natural logarithm of x";
- XMessage "\pV mLn1 -- computes the natural logarithm of (1+x)";
- XMessage "\pV mExp2 -- computes 2^x";
- XMessage "\pV mExp -- computes e^x";
- XMessage "\pV mExp1 -- computes (e^x)-1";
- XMessage "\pV mCos -- computes the cosine of x";
- XMessage "\pV mSin -- computes the sine of x";
- XMessage "\pV mTan -- computes the tangent of x";
- XMessage "\pV mAtan -- computes the arctangent of x";
- XMessage "\pV mRandomX -- computes a pseudorandom value with x as seed";
- XMessage "\pV mLog21 -- computes log base 2 of (1+x)";
- XMessage "\pV mExp21 -- computes (2^x)-1";
- XMessagesEnd
-
- /*--- method dispatch table ---*/
- XMethodsBegin
- XMethod MethodNew;
- XMethod MethodLog2;
- XMethod MethodLn;
- XMethod MethodLn1;
- XMethod MethodExp2;
- XMethod MethodExp;
- XMethod MethodExp1;
- XMethod MethodCos;
- XMethod MethodSin;
- XMethod MethodTan;
- XMethod MethodAtan;
- XMethod MethodRandomX;
- XMethod MethodLog21;
- XMethod MethodExp21;
- XMethodsEnd
- }
-
-
-
- /*-----------------------------------------------------------------------*/
- /* mNew is called AFTER the generic instance allocation, so we already */
- /* have an instance, but we have to resize it according to what we */
- /* need in terms of storage. We also do any other object initialization */
- /* at this point. Returns zero for successful initialization, non-zero */
- /* for any errors. */
- /*-----------------------------------------------------------------------*/
- pascal long MethodNew(MsgId message, FPObj me)
- { short er; /* returns possible error from memory manager */
-
- XMethodOpen(me);
- SetHandleSize((Handle)me, sizeof(**me));
- if (!(er=MemError())) {
- /*--- memory alloc ok, now do other init ---*/
- }
- XMethodClose(me);
- return (er);
- }
-
-
- /*--------------------------------------------------------------------*/
- /* Allocate a handle to an extended floating point number, and return */
- /* that handle. Returns 0 if the handle can't be allocated. */
- /*--------------------------------------------------------------------*/
- Handle extended_to_handle(extended num)
- { extended *p;
- Handle h=NewHandle(sizeof(num));
- if (h==NULL) return h;
- p=(extended *)*h;
- *p=num;
- return h;
- }
-
-
- /*---------------------------------------------------------------------------*/
- /* Trig function attempt: the sine function. Passing floating point args */
- /* as variable parameters... */
- /*---------------------------------------------------------------------------*/
- pascal long float_methods(int fn, long argc, TyValuePtr argv, MsgId message, FPObj me)
- { extended x;
- extended *pargument,*presult;
- Handle h;
- TyValue rv;
- XMethodOpen(me);
- /*--- make sure we have just one argument ---*/
- if (argc!=1) {
- rv.tvType=4;
- rv.tvValue=333333;
- goto EXIT;
- }
-
- /*--- switch based on argument type ---*/
- switch(argv[1].tvType) {
- case TY_EXTENDED:
- pargument=*((extended **)argv[1].tvValue);
- x=*pargument;
- rv.tvType=TY_EXTENDED;
- switch(fn) {
- case FN_Log2: rv.tvValue=(long)extended_to_handle(log2(x)); break;
- case FN_Ln: rv.tvValue=(long)extended_to_handle(ln(x)); break;
- case FN_Ln1: rv.tvValue=(long)extended_to_handle(ln1(x)); break;
- case FN_Exp2: rv.tvValue=(long)extended_to_handle(exp2(x)); break;
- case FN_Exp: rv.tvValue=(long)extended_to_handle(exp(x)); break;
- case FN_Exp1: rv.tvValue=(long)extended_to_handle(exp1(x)); break;
- case FN_Cos: rv.tvValue=(long)extended_to_handle(cos(x)); break;
- case FN_Sin: rv.tvValue=(long)extended_to_handle(sin(x)); break;
- case FN_Tan: rv.tvValue=(long)extended_to_handle(tan(x)); break;
- case FN_Atan: rv.tvValue=(long)extended_to_handle(atan(x)); break;
- case FN_RandomX: rv.tvValue=(long)extended_to_handle(randomx(&x)); break;
- case FN_Log21: rv.tvValue=(long)extended_to_handle(log21(x)); break;
- case FN_Exp21: rv.tvValue=(long)extended_to_handle(exp21(x)); break;
- }
- break;
- default:
- rv.tvType=4;
- rv.tvValue=444444;
- } /* switch */
-
- EXIT:
- /*--- set return values. Filter for bad handles ---*/
- if ((rv.tvType==TY_EXTENDED)&&(rv.tvValue==(long)NULL)) {
- rv.tvType=TY_LONGINT; rv.tvValue=999999;
- }
- argv[0].tvType=rv.tvType;
- argv[0].tvValue=rv.tvValue;
- XMethodClose(me);
- /*--- return value is ignored ---*/
- return 0;
- }
-
-
- pascal long MethodLog2(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Log2,argc,argv,message,me); }
-
- pascal long MethodLn(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Ln,argc,argv,message,me); }
-
- pascal long MethodLn1(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Ln1,argc,argv,message,me); }
-
- pascal long MethodExp2(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Exp2,argc,argv,message,me); }
-
- pascal long MethodExp(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Exp,argc,argv,message,me); }
-
- pascal long MethodExp1(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Exp1,argc,argv,message,me); }
-
- pascal long MethodCos(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Cos,argc,argv,message,me); }
-
- pascal long MethodSin(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Sin,argc,argv,message,me); }
-
- pascal long MethodTan(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Tan,argc,argv,message,me); }
-
- pascal long MethodAtan(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Atan,argc,argv,message,me); }
-
- pascal long MethodRandomX(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_RandomX,argc,argv,message,me); }
-
- pascal long MethodLog21(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Log21,argc,argv,message,me); }
-
- pascal long MethodExp21(long argc, TyValuePtr argv, MsgId message, FPObj me)
- { float_methods(FN_Exp21,argc,argv,message,me); }
-
- /*--- end of FP_main.c ---*/
-